//
// Created by xili on 2024/9/2 10:09.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int maxConsecutiveAnswers(string answerKey, int k) {
        return max(maxConsecutiveChar(answerKey, k, 'T'), maxConsecutiveChar(answerKey, k, 'F'));
    }

private:
    int maxConsecutiveChar(const string &s, int k, char ch) {
        int left = 0, right = 0, max_len = 0, count = 0;
        while (right < s.size()) {
            if (s[right] != ch) count++;
            while (count > k) {
                if (s[left] != ch) count--;
                left++;
            }
            max_len = max(max_len, right - left + 1);
            right++;
        }

        return max_len;
    }
};
